# -*-coding:utf8-*-
import sys
reload(sys)  
sys.setdefaultencoding('utf8')

import requests
from lxml import etree

import datetime
import win32clipboard as w
import win32con

url="https://cn.investing.com/indices/major-indices"
page=requests.get(url)
html=page.text

# fout = open(r'test.html', 'w')
# fout.write(html)
# fout.flush()
# fout.close()

selector = etree.HTML(html)

number = '40820'

index_name = selector.xpath('//tr[@id="pair_'+number+'"]/td[@class="bold left noWrap elp plusIconTd"]/a/text()')[0]
last_price = selector.xpath('//tr[@id="pair_'+number+'"]/td[@class="pid-'+number+'-last"]/text()')[0]
highest_price = selector.xpath('//tr[@id="pair_'+number+'"]/td[@class="pid-'+number+'-high"]/text()')[0]
lowest_price = selector.xpath('//tr[@id="pair_'+number+'"]/td[@class="pid-'+number+'-low"]/text()')[0]
net_change = selector.xpath('//tr[@id="pair_'+number+'"]/td[@class="bold redFont pid-'+number+'-pc"]/text()')[0]
change_percent = selector.xpath('//tr[@id="pair_'+number+'"]/td[@class="bold redFont pid-'+number+'-pcp"]/text()')[0]
time_date = selector.xpath('//tr[@id="pair_'+number+'"]/td[@class=" pid-'+number+'-time"]/text()')[0] + "/" + datetime.datetime.now().strftime('%Y')
date = datetime.datetime.strptime(time_date, "%d/%m/%Y")

# print index_name
# print last_price
# print highest_price
# print lowest_price
# print net_change
# print change_percent
# print time_date
# print date

'''
组合成所需的消息
'''
def rise_down(string):
    ret_code = "00"
    if string[-1] == "%":
        if string[0] == "-":
            ret_code = "01"
        else:
            ret_code = "02"
    else: 
        if string[0] == "-":
            ret_code = "03"
        else:
            ret_code = "04"
    return ret_code

word_dict = {
    "01": "跌幅",
    "02": "涨幅",
    "03": "下跌",
    "04": "上涨",
}

text = index_name+str(date.strftime('%m月%d日'))+word_dict[rise_down(net_change)]+net_change[1:]+"，"+word_dict[rise_down(change_percent)]+change_percent[1:]+"，收盘"+last_price.replace(",", "")+"。"
print text

w.OpenClipboard()
w.EmptyClipboard()
w.SetClipboardData(win32con.CF_UNICODETEXT, text)
w.CloseClipboard()
